package cn.hbu.zlfw.exchange.controller;


import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hbu.zlfw.admin.entity.Expert;
import cn.hbu.zlfw.admin.entity.ScienceExpert;
import cn.hbu.zlfw.annotation.OperatorLog;
import cn.hbu.zlfw.annotation.OperatorType;
import cn.hbu.zlfw.common.util.SessionUtil;
import cn.hbu.zlfw.exchange.entity.Poster;
import cn.hbu.zlfw.exchange.entity.Replier;
import cn.hbu.zlfw.exchange.service.PosterService;
/**
 * 
 * @ClassName: ReplierController 
 * @Description: 留言板子贴控制层
 * @author 王鹏伟<1223443289@qq.com>
 * @date 2017年12月19日 下午9:55:11 
 * @vsersion:v1.0
 */
import cn.hbu.zlfw.exchange.service.ReplierService;
import cn.hbu.zlfw.research.entity.Researcher;

@RequestMapping("/replier")
@Controller
public class ReplierController {
	/**
	 * 日志
	 */
	public final Logger logger=LoggerFactory.getLogger(ReplierController.class);
	/**
	 * 注入HttpServletRequest
	 */
	@Autowired
	private HttpServletRequest request;
	/**
	 * 注入service
	 */
	@Autowired
	private ReplierService replierService;
	@Autowired
	private PosterService posterService;
	
	/**
	 * 
	 * @Title: replyQuestionUI 
	 * @Description: 转向回复问题页面
	 * @return String 
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="转向回复问题页面")
	@RequestMapping({"/replyQuestionUI.action","/replyQuestionUI.do"})
	public String replyQuestionUI(@RequestParam(value="posterId",required=false)String posterId,
			@RequestParam(value="floor",required=false)Integer floor,ModelMap map) {
		map.put("posterId", posterId);
		map.put("floor", floor);
		String url = request.getRequestURL().toString();
		if(url.contains(".do")) {
			return "post/adminreplyQuestion";
		} else  {
			return "post/replyQuestion";
		}
		
	}
	
	/**
	 * 
	 * @Title: replyQuestion 
	 * @Description: 保存回复问题信息
	 * @return String 
	 * @throws
	 */
	@ResponseBody
	@OperatorLog(type=OperatorType.SAVE,description="保存回复问题信息")
	@RequestMapping({"/replyQuestion.action","/replyQuestion.do"})
	public String replyQuestion(@ModelAttribute("replier")Replier replier,
			@RequestParam(value="posterId",required=false)String posterId) {
		Poster poster = this.posterService.getById(posterId);
		replier.setPoster(poster);
		replier.setCreateTime(new Date());
		replier.setUpdateTime(new Date());
		replier.setDelState(0);
		Object obj = SessionUtil.getLoginUser(request);
		if(obj instanceof ScienceExpert) {
			ScienceExpert scienceExpert = (ScienceExpert)obj;
			replier.setUserName(scienceExpert.getUserName());
			replier.setUserType(scienceExpert.getUserType());
			replier.setDepartment(scienceExpert.getDepartment());
			replier.setSchoolType(scienceExpert.getSchoolType());
			this.replierService.save(replier);
			return "success";
		} else if (obj instanceof Researcher) {
			Researcher researcher = (Researcher)obj;
			replier.setUserName(researcher.getUserName());
			replier.setUserType(researcher.getUserType());
			replier.setDepartment(researcher.getDepartment());
			replier.setSchoolType(researcher.getSchoolType());
			this.replierService.save(replier);
			return "success";
		} else if (obj instanceof Expert) {
			Expert expert = (Expert)obj;
			replier.setUserName(expert.getUserName());
			replier.setUserType(expert.getUserType());
			replier.setDepartment(expert.getDepartment());
			replier.setSchoolType(expert.getSchoolType());
			this.replierService.save(replier);
			return "success";
		} else {
			return "fail";
		}
	}

}
